<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="default" :inline="true" label-width="68px">
      <el-form-item label="报告编号" prop="reportNumber">
        <el-input
          v-model="queryParams.reportNumber"
          placeholder="请输入报告编号"
          clearable
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-perms="['qa:apprasial_reports:add']"
          v-preventReClick
        >新增
        </el-button>
      </el-col>
    </el-row>

    <el-table v-loading="loading" :data="apprasial_reportsList">
      <el-table-column label="主键id" align="center" prop="id" />
      <el-table-column label="报告编号" align="center" prop="reportNumber" />
      <el-table-column label="鉴定对象" align="center" prop="appraisalObject" />
      <el-table-column label="鉴定结果" align="center" prop="appraisalResult" />
      <el-table-column label="鉴定人id" align="center" prop="appraiserId" />
      <el-table-column label="鉴定日期" align="center" prop="appraisalDate" width="180" />
      <el-table-column label="状态" align="center" prop="status">
        <template #default="scope">
          <status-tag :status-enum="AuditStatus" :status="scope.row.status"/>
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button
            type="primary"
            link
            icon="Edit"
            @click="handleUpdate(scope.row)"
            v-perms="['qa:apprasial_reports:edit']"
            v-preventReClick
          >修改
          </el-button>
          <el-button
            type="primary"
            link
            icon="Delete"
            @click="handleDelete(scope.row)"
            v-perms="['qa:apprasial_reports:remove']"
            v-preventReClick
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-pagination
      v-if="total>0"
      v-model:current-page="queryParams.pageNum"
      :page-size="10"
      layout="total, prev, pager, next"
      :total="total"
      style="float: right"
    />

    <!-- 添加或修改鉴定报告对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="案件" prop="caseId">
          <el-select v-model="form.caseId" placeholder="请选择案件id">
            <el-option
              v-for="item in caseList"
              :key="item.id"
              :label="`${item.consulationId}-${item.createTime}`"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="报告编号" prop="reportNumber">
          <el-input v-model="form.reportNumber" placeholder="请输入报告编号" />
        </el-form-item>
        <el-form-item label="鉴定对象" prop="appraisalObject">
          <el-input v-model="form.appraisalObject" placeholder="请输入鉴定对象" />
        </el-form-item>
        <el-form-item label="鉴定结果" prop="appraisalResult">
          <el-input v-model="form.appraisalResult" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="鉴定人id" prop="appraiserId">
          <el-input v-model="form.appraiserId" placeholder="请输入鉴定人id" />
        </el-form-item>
        <el-form-item label="鉴定日期" prop="appraisalDate">
          <el-date-picker clearable
                          v-model="form.appraisalDate"
                          type="datetime"
                          format="YYYY-MM-DD HH:mm:ss"
                          value-format="YYYY-MM-DD HH:mm:ss"
                          placeholder="请选择鉴定日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <el-form-item label="状态" prop="status">
        <el-radio-group v-model="form.status">
          <el-radio v-for="item in convertToOptions(AuditStatus)" :key="item.value" size="large"
                    :value="item.value.toString()">{{ item.label }}
          </el-radio>
        </el-radio-group>
      </el-form-item>
      <template #footer>
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script>

import { ApprasialReportsService } from '@/api/qa/apprasial_reports.js'
import { listCase_registration } from '@/api/LegalCase/case_registration.js'
import { convertToOptions } from '@/utils/util.js'
import { AuditStatus } from '@/enum.js'
import StatusTag from '@/components/StatusTag.vue'

export default {
  name: 'Apprasial_reports',
  components: { StatusTag },
  computed: {
    AuditStatus() {
      return AuditStatus
    }
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 总条数
      total: 0,
      // 鉴定报告表格数据
      apprasial_reportsList: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        reportNumber: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        reportNumber: [
          { required: true, message: '鉴定对象不能为空', trigger: 'blur' }
        ],
        appraisalObject: [
          { required: true, message: '鉴定对象不能为空', trigger: 'blur' }
        ],
        appraisalResult: [
          { required: true, message: '鉴定结果不能为空', trigger: 'blur' }
        ],
        appraiserId: [
          { required: true, message: '鉴定人id不能为空', trigger: 'blur' }
        ],
        appraisalDate: [
          { required: true, message: '鉴定日期不能为空', trigger: 'blur' }
        ]
      },
      caseList:[]
    }
  },
  created() {
    this.getList()
    this.getCaseList()
  },
  methods: {
    convertToOptions,
    getCaseList(){
      listCase_registration().then(resp=>{
        this.caseList = resp.rows
      })
    },
    /** 查询鉴定报告列表 */
    getList() {
      if (this.$route.query.id) {
        this.queryParams.caseId = this.$route.query.id
      }
      this.loading = true
      ApprasialReportsService.listApprasial_reports(this.queryParams).then(response => {
        this.apprasial_reportsList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        reportNumber: null,
        appraisalObject: null,
        appraisalResult: null,
        appraiserId: null,
        appraisalDate: null,
        remark: null,
        createTime: null,
        updateTime: null
      }
      this.resetForm('form')
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.$refs.queryForm.resetFields()
      this.handleQuery()
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.open = true
      this.title = '添加鉴定报告'
      this.reset()
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      const id = row.id || this.ids
      ApprasialReportsService.getApprasial_reports(id).then(response => {
        this.form = response.data
        this.open = true
        this.title = '修改鉴定报告'
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            ApprasialReportsService.updateApprasial_reports(this.form).then(response => {
              this.$message.success('修改成功')
              this.open = false
              this.getList()
            })
          } else {
            ApprasialReportsService.addApprasial_reports(this.form).then(response => {
              this.$message.success('新增成功')
              this.open = false
              this.getList()
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids
      this.$confirm('是否确认删除鉴定报告编号为"' + ids + '"的数据项？').then(function() {
        return ApprasialReportsService.delApprasial_reports(ids)
      }).then(() => {
        this.getList()
        this.$message.success('删除成功')
      }).catch(() => {
      })
    },
  }
}
</script>
